package com.xjyandkj.socialcalendar.adapter.calendar;

import java.util.Calendar;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xjyandkj.socialcalendar.R;
import com.xjyandkj.socialcalendar.ui.MainActivity;

public class CalendarAdapter extends BaseAdapter{

	int week;//当月第一天是星期几
	int monthDays;//当月多少天
	int currentDay, currentMonth, currentYear;//当天的所在年月日
	Calendar cal = Calendar.getInstance();//用来获取星期信息的对象
	Calendar today = Calendar.getInstance(); // 用来获取当前日期的对象 
	//  定义Context 
	private Context mContext; 
	private String[] number = new String[42];//  日期数组 

	public CalendarAdapter(Context c, int currentYear, int currentMonth, int currentDay) {
		mContext = c;
		this.currentDay = currentDay;
		this.currentMonth = currentMonth;
		this.currentYear = currentYear;
		getweek(currentYear, currentMonth);
	}

	public int getCount() {
		return number.length;
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {// 第一屏
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.calendar_calendaritem, null);
		}
		TextView textview = (TextView) convertView.findViewById(R.id.tvtext);
		String strpos = number[position];
		textview.setText(strpos); // 当前月信息显示
		if (position <= monthDays + week && position > week) {
			textview.setTextColor(Color.BLACK);// 当月字体设黑
			if (Integer.parseInt(number[position]) == currentDay &&
					today.get(Calendar.MONTH) == currentMonth &&
					today.get(Calendar.YEAR) == currentYear){
				textview.setBackgroundColor(Color.BLUE);//当天设置背景蓝色
			}
			//当月的日期，点击后会跳转到日期显示界面
			textview.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					Intent i = new Intent(mContext, MainActivity.class);
					i.putExtra("day", ((TextView)v).getText());
					mContext.startActivity(i);
				}
			});
		} else {
			textview.setTextColor(Color.GRAY);// 其他月字体设灰
			//其他月的日期，则会跳转到那个月(暂时不实现)
		}
		return convertView;
	}

	// 获得天数
	private int getMonthDays(int year, int month) {
		month++;
		switch (month) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12: {
				return 31;
			}
			case 4:
			case 6:
			case 9:
			case 11: {
				return 30;
			}
			case 2: {
				if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
					return 29;
				else
					return 28;
			}
		}
		return 0;
	}

	private void getweek(int year, int month) { 
		// 将当前日历设为指定月份的第一天
		cal.set(year, month, 1); 
		// 获得指定月份的第1 天是当前周的第几天
		week = cal.get(Calendar.DAY_OF_WEEK) - 2;
		// System.out.println("week=" + week);
		// 获得当前月有多少天
		monthDays = getMonthDays(year, month);
		int lastmonthDays;
		if(month != 0) {
			lastmonthDays = getMonthDays(year, month - 1);// 上月
		} else {
			lastmonthDays = getMonthDays(year-1, 11);
		}
		
		for (int i = 0; i < number.length; i++) {
			// 周一
			if (i <= week) {
				int temp = lastmonthDays - week;
				number[i] = (temp + i) + "";
			} else if (i <= monthDays + week) {
				number[i] = i - week + "";
			} else {
				for (int j = 0; j < number.length - i; j++) {
					int temp = number.length - monthDays - week;
					number[i] = temp - (j + 1) + "";
				}
			}
		}
	}

}
